﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Newtonsoft.Json.Linq;
using System.IO;

namespace WindowsPhoneAppHost
{
    public partial class Process : PhoneApplicationPage
    {
        public Process()
        {
            InitializeComponent();
            //DoHttpWebRequest();
        }

        //*
        private void DoHttpWebRequest()
        {
            //string url = "http://localhost/MobileOA/Process.js";
            string url = "http://localhost/MobileOA/Process.js";
            WebRequest request = WebRequest.Create(url); //创建WebRequest类
            IAsyncResult result = request.BeginGetResponse(ResponseCallback, request); //返回异步操作的状态
        }

        private void ResponseCallback(IAsyncResult result)
        {
            var request = (HttpWebRequest)result.AsyncState; //获取异步操作返回的的信息
            WebResponse response = request.EndGetResponse(result); //结束对 Internet 资源的异步请求
            using (Stream stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                string contents = reader.ReadToEnd();
                //Dispatcher.BeginInvoke(delegate() { this.textBlock1.Text = contents; });
                Dispatcher.BeginInvoke(delegate { ParseJson(contents); });
            }
        }

        private void ParseJson(string jsonString)
        {
            JObject obj = JObject.Parse(jsonString);
            ParseRoutes((JArray)obj["routes"]);
            //ParseNodes((JArray)obj["nodes"]);
            //ParseComments((JArray)obj["comments"]);

            /*
            Button button = new Button();
            button.Content = "提交";
            //button.Click += 
            stackPanel1.Children.Add(button);
            */
            /*
            Debug.WriteLine(jsonString);
            JArray jArray = JArray.Parse(jsonString);
            Debug.WriteLine(jArray.Count.ToString());

            for (int i = 0; i < jArray.Count; i++)
            {
                var list = (JArray)jArray[i];
                if (0 != i)
                {
                    listBox.Items.Add(new ListSeperator());
                    listBox.Items.Add(new ListSeperator());
                    listBox.Items.Add(new ListSeperator());
                }
                listBox.Items.Add(new ListSeperator());
                for (int j = 0; j < list.Count; j++)
                {
                    var listobj = (JObject)list[i];
                    FormListBoxItem formListBoxItem = new FormListBoxItem();
                    formListBoxItem.Title = Common.getJsonString(listobj, "title");
                    formListBoxItem.Value = Common.getJsonString(listobj, "value");
                    listBox.Items.Add(formListBoxItem);
                    listBox.Items.Add(new ListSeperator());
                }
            }
             */
        }
        private void ParseRoutes(JArray array)
        {
            if (null != array)
            {
                //int index = stackPanel1.Children.Count - 1;
                //ProcessTitle processTitle = new ProcessTitle();
                //processTitle.TitleText = "请选择处理节点：";
                //stackPanel1.Children.Insert(index, processTitle);
                for (int i = 0; i < array.Count; i++)
                {
                    JObject obj = (JObject)array[i];
                    string str = Common.getJsonString(obj, "text");
                    this.listBox1.Items.Add(str);
                }
            }
        }
        private void ParseNodes(JArray array)
        {
            if (null != array)
            {
                //int index = stackPanel1.Children.Count - 1;
                //ProcessTitle processTitle = new ProcessTitle();
                //processTitle.TitleText = "项目经理审核:";
                //stackPanel1.Children.Insert(index, processTitle);
                //*
                //for (int i = 0; i < array.Count; i++)
                {
                    JObject node = (JObject)array[0];
                    //Common.getJsonString(object, "text");
                    JArray names = (JArray)node["names"];
                    for (int j = 0; j < names.Count; j++)
                    {
                        JObject name = (JObject)array[j];
                        string str = Common.getJsonString(name, "name");
                        this.listBox2.Items.Add(str);
                    }
                }
                //*/
            }
        }

        private void ParseComments(JArray array)
        {
            int index = stackPanel1.Children.Count - 1;
            ProcessTitle processTitle = new ProcessTitle();
            processTitle.TitleText = "处理意见:";
            stackPanel1.Children.Insert(index, processTitle);
            //*
            for (int i = 0; i < array.Count; i++)
            {
                JObject obj = (JObject)array[i];
                string str = Common.getJsonString(obj, "text");
                //stackPanel1.Children.Insert(++index, processTitle);
            }
            //* */
        }
        //*/
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            //这里可以执行弹出对话框操作
            e.Cancel = true;
            MessageBox.Show("返回键不再返回");
        }
    }
}